package cn.ljy.uem.service.impl;

import cn.ljy.authority.model.SysDepartment;
import cn.ljy.authority.service.impl.IBaseService;
import cn.ljy.common.exception.model.MyWebException;
import cn.ljy.uem.dao.SchoolDepartmentMapper;
import cn.ljy.uem.dao.SchoolMajorGroupMapper;
import cn.ljy.uem.dao.SchoolMajorMapper;
import cn.ljy.uem.dao.SchoolTeacherMapper;
import cn.ljy.uem.model.*;
import cn.ljy.uem.model.query.SchoolMajorQuery;
import cn.ljy.uem.service.SchoolMajorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 专业类
 *  author 王秋林   修改
 *  date 2022/07/24
 */
@Service
@Slf4j
public class ISchoolMajorService extends IBaseService<SchoolMajor, SchoolMajorQuery> implements SchoolMajorService {

    @Autowired
    private SchoolMajorMapper mapper;
    @Autowired
    private SchoolDepartmentMapper schoolDepartmentMapper;
    @Autowired
    private SchoolMajorGroupMapper schoolMajorGroupMapper;
    @Autowired
    private SchoolTeacherMapper schoolTeacherMapper;

    @Override
    protected void beforeGet(Serializable id) {
        if (mapper.findById(id) == null){
            throw new MyWebException("查找失败：找不到指定记录");
        }
    }

    @Override
    protected void beforeUpdate(SchoolMajor schoolMajor) {
        if (mapper.findById(schoolMajor.getId()) == null){
            throw new MyWebException("编辑失败：找不到指定记录");
        }
        //判断专业添加部门是否存在
        if(schoolDepartmentMapper.findById(schoolMajor.getDepartmentId())==null){
            throw new MyWebException("添加失败：该部门不存在");
        }
    }

    @Override
    protected void beforeSave(SchoolMajor schoolMajor) {
        if (schoolMajor.getDepartmentId() == null){//判断专业对应部门是否为空
            throw new MyWebException("添加失败：找不到指定记录");
        }
        //判断专业是否重复
        if(mapper.countByName(schoolMajor.getName())>0){
            throw new MyWebException("添加失败：该专业已存在");
        }
        //判断专业添加部门是否存在
        if(schoolDepartmentMapper.findById(schoolMajor.getDepartmentId())==null){
            throw new MyWebException("添加失败：该部门不存在");
        }
    }

    @Override
    protected void beforeDelete(Serializable[] ids) {

    }
    @Override
    protected void afterGet(SchoolMajor schoolMajor) {
        if (schoolMajor.getDepartmentId() != null) {
            //查找专业所属部门
            SchoolDepartment schoolDepartment = schoolDepartmentMapper.findById(schoolMajor.getDepartmentId());
            //赋值
            schoolMajor.setSchoolDepartment(schoolDepartment);
        }
        //添加专业群关联属性
        if (schoolMajor.getMajorGroupId() != null) {
            //查找专业所属专业群
            SchoolMajorGroup schoolMajorGroup = schoolMajorGroupMapper.findById(schoolMajor.getMajorGroupId());
            //赋值
            schoolMajor.setSchoolMajorGroup(schoolMajorGroup);
        }
        //添加专业教师关联属性
        if (schoolMajor.getTeacherId() != null) {
            //查找专业教师信息
            SchoolTeacher schoolTeacher = schoolTeacherMapper.findById(schoolMajor.getTeacherId());
            //赋值
            schoolMajor.setSchoolTeacher(schoolTeacher);
        }
        //添加专业的教师数量
        schoolMajor.setTeacherNumber(schoolTeacherMapper.countAllTeacherByMajorId(schoolMajor.getId()));
    }

    @Override
    protected void afterFindAll(List<SchoolMajor> list) {

        for (SchoolMajor schoolMajor : list) {
            //添加部门关联属性
            if (schoolMajor.getDepartmentId() != null) {
                //查找专业所属部门
                SchoolDepartment schoolDepartment = schoolDepartmentMapper.findById(schoolMajor.getDepartmentId());
                //赋值
                schoolMajor.setSchoolDepartment(schoolDepartment);
            }
            //添加专业群关联属性
            if (schoolMajor.getMajorGroupId() != null) {
                //查找专业所属专业群
                SchoolMajorGroup schoolMajorGroup = schoolMajorGroupMapper.findById(schoolMajor.getMajorGroupId());
                //赋值
                schoolMajor.setSchoolMajorGroup(schoolMajorGroup);
            }
            //添加专业负责人信息
            if (schoolMajor.getTeacherId() != null) {
                //查找专业负责人信息
                SchoolTeacher schoolTeacher = schoolTeacherMapper.findById(schoolMajor.getTeacherId());
                //赋值
                schoolMajor.setSchoolTeacher(schoolTeacher);
            }
            //添加专业的教师数量
            schoolMajor.setTeacherNumber(schoolTeacherMapper.countAllTeacherByMajorId(schoolMajor.getId()));
        }
    }


}
